const enum LogLevel {
    All = 0,
    Trace = 1,
    Debug = 2,
    Information = 3,
    Warning = 4,
    Error = 6,
    Critical = 7,
    None = 8
}

const styles: Map<LogLevel, string> = new Map(
    [
        [LogLevel.Trace, "color:white;"],
        [LogLevel.Debug, "color:gray;"],
        [LogLevel.Information, "color:green;"],
        [LogLevel.Warning, "color:yellow;"],
        [LogLevel.Error, "color:red;"],
        [LogLevel.Critical, "color:darkred;"],
    ]
);

export default class Logger implements Chaos.Logger {
    private static instance: Logger = new Logger();
    /**
     * Window变量加载
     */
    public static get Default(): Logger {
        return this.instance;
    }

    Level: number = 0;

    private Log(level: number, label: string, message: Object): void {
        if (level < this.Level) {
            return;
        }
        console.log(`%c${new Date().format('yyyyMMdd hh:mm:ss:fff')} ${label.padEnd(12, ' ')} ${message instanceof Object ? JSON.stringify(message) : message}`, styles.get(level));
    }

    SetLevel(level: number): void {
        this.Level = level;
        console.log(`日志级别:${level}`);
    }
    Trace(message: Object): void {
        this.Log(LogLevel.Trace, 'Trace', message);
    }
    Debug(message: Object): void {
        this.Log(LogLevel.Debug, 'Debug', message);
    }
    Information(message: Object): void {
        this.Log(LogLevel.Information, 'Information', message);
    }
    Warning(message: Object): void {
        this.Log(LogLevel.Warning, 'Warning', message);
    }
    Error(message: Object): void {
        this.Log(LogLevel.Error, 'Error', message);
    }
    Critical(message: Object): void {
        this.Log(LogLevel.Critical, 'Critical', message);
    }

    public static SetLevel(level: number): void {
        this.instance.Level = level;
        console.log(`日志级别:${level}`);
    }
    public static Trace(message: Object): void {
        this.instance.Log(LogLevel.Trace, 'Trace', message);
    }
    public static Debug(message: Object): void {
        this.instance.Log(LogLevel.Debug, 'Debug', message);
    }
    public static Information(message: Object): void {
        this.instance.Log(LogLevel.Information, 'Information', message);
    }
    public static Warning(message: Object): void {
        this.instance.Log(LogLevel.Warning, 'Warning', message);
    }
    public static Error(message: Object): void {
        this.instance.Log(LogLevel.Error, 'Error', message);
    }
    public static Critical(message: Object): void {
        this.instance.Log(LogLevel.Critical, 'Critical', message);
    }
}